SpringCloud 入门笔记(二)Eureka服务注册中心

概述

Eureka是一个服务注册中心,分为Eureka Server和Eureka Client,Server和Client均是SpringBoot应用程序,其中Client即为各个微服务,其需要向Server进行注册,并发送心跳来维护活跃,下面开始构建一个简单的Eureka服务注册环境。

Eureka Server

构建Server项目

通过IDEA的Spring Initializr创建一个包含Eureka Server组件依赖的SpringBoot项目,如下图所示勾选Eureka Server依赖,填写项目的相关信息一路next到finish。

本例创建了一个名为eureka-server的SpringBoot项目,创建成功后,IDEA会自动为我们添加Eureka Server依赖,如下:

1
2
3
4
5
6
7
8
9
10
11
12
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

下面,对项目进行一些配置,启用Eureka Server的功能。

首先,编写 application.yml 配置文件,内容如下:

1
2
3
4
5
6
7
8
9
10
server:
port: 8000
eureka:
instance:
hostname: 127.0.0.1 # 服务注册中心IP地址
client:
registerWithEureka: false # 向服务注册中心注册自己
fetchRegistry: false # 检索服务
service-url: # 指定服务注册中心的位置
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

由于配置的是server,不需要将server本身注册到服务中心,因此需要关闭一些默认为true的配置,最终的eureka注册地址为:http://127.0.0.1:8000/eureka,这也是client进行服务注册的请求地址。

然后,修改启动类,添加 @EnableEurekaServer 注解,如下:

1
2
3
4
5
6
7
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

至此,一个单点的Eureka Server就搭建完成了,在浏览器输入localhost:8000,可以看到如下界面:

但是我们会发现,后台界面不需要任何认证即可访问,接下来,我们来为Server添加Spring Security认证

添加Spring Security认证

在pom.xml添加如下依赖:

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

修改 application.yml 配置文件,启用Spring Security,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server:
port: 8000
spring:
security:
user:
name: admin
password: admin123
eureka:
instance:
hostname: 127.0.0.1 # 服务注册中心IP地址
client:
registerWithEureka: false # 是否向服务注册中心注册自己
fetchRegistry: false # 是否检索服务
service-url: # 服务注册中心的配置内容,指定服务注册中心的位置
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/

需要注意的是,添加Security认证后,注册位置变成 http://admin:admin123@127.0.0.1:8000/eureka

修改启动类,添加@EnableWebSecurity注解,如下:

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableEurekaServer
@EnableWebSecurity
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

网上很多的教程均是通过此种方式进行配置的,但这种方式存在一个问题:Client无法向Server注册服务,错误信息为 Cannot execute request on any known server,具体可以参考SpringCloud下的ISSUE spring boot 2.0,eureka registration failed with spring security

原因为:SpringBoot 2.0版本起,Security中默认启用了CSRF保护,需要关闭CSRF保护。

新建Security配置类,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.csrf().disable()
.authorizeRequests()
.anyRequest().fullyAuthenticated()
.and()
.httpBasic();
}
}

这里需要注意的是,如果直接通过 http.csrf().disable(); 关闭SCRF保护(网上很多方案均是如此),那么也将关闭其他请求的认证,因此需要为其他的请求配置需要认证。

OK,到这里,Server的配置就完成了,再次访问后台界面时,浏览器就会弹出登录认证了。

Eureka Client

构建Client项目

通过IDEA的Spring Initializr创建一个包含Eureka Discovery组件依赖的SpringBoot项目,勾选如下图所示的Eureka Discovery依赖。Eureka Client即为各个微服务,下面创建一个示例服务eureka-client。

创建成功后,pom.xml依赖如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

编写 application.yml 配置文件如下:

1
2
3
4
5
6
7
8
9
server:
port: 8801
spring:
application:
name: eureka-client
eureka:
client:
service-url:
defaultZone: http://admin:admin123@127.0.0.1:8000/eureka/

修改启动类,添加@EnableEurekaClient注解,表示这是一个Eureka Client应用,如下所示:

1
2
3
4
5
6
7
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(UserMsApplication.class, args);
}
}

测试

  1. 启动 eureka-server 项目

  2. 启动 eureka-client 项目

  3. 访问 eureka-server,localhost:8800,输入我们配置的用户名和密码,就可以看到 eureka-client 已经注册到server中了

Eureka Server 集群

上面构建的 Eureka Server 是一个单点部署的服务注册中心,这种方式在服务注册中心崩溃时会导致整个系统的瘫痪,通常hi使用集群化部署的方式来避免这个问题。

Eureka Server 集群的配置与单点配置大致相同,唯一的不同之处在于需要在每个 Server 的配置中指定集群中其他 Server 节点的地址,如下所示:

Server1配置:

1
2
3
4
5
6
7
8
9
10
server:
port: 8000
eureka:
instance:
hostname: server1
client:
registerWithEureka: false
fetchRegistry: false
service-url:
defaultZone: http://server2:8001/eureka/,http://server3:8002/eureka/

Server2配置:

1
2
3
4
5
6
7
8
9
10
server:
port: 8001
eureka:
instance:
hostname: server2
client:
registerWithEureka: false
fetchRegistry: false
service-url:
defaultZone: http://server1:8000/eureka/,http://server3:8002/eureka/

Server3配置:

1
2
3
4
5
6
7
8
9
10
server:
port: 8002
eureka:
instance:
hostname: server2
client:
registerWithEureka: false
fetchRegistry: false
service-url:
defaultZone: http://server1:8000/eureka/,http://server2:8001/eureka/

按照如上配置,分别启动多个eureka-server实例即可启动集群化部署。

Client 在指定 Server 注册地址时,可以指定一个或多个地址,多个地址间用”,”分隔。

源码地址:https://github.com/GreedyStar/spring-cloud-demo

最后的最后,安利一下自己写的一个Java代码生成工具,能够方便的生成Spring、SpringMVC、Mybatis架构下的Java代码,希望能对大家有所帮助,地址:Java代码生成器:Generator